/**
 * Copyright(C)版权所有 中国电子科技集团公司第三十二研究所
 * @Title: VfileMenuController数据管理目录操作接口
 * @author: xiao
 * @version: 1.0
 * @date: 2020/10/14
 * 备注:无
 */
package com.cetc32.dh.controller.rest;

import com.cetc32.dh.common.response.ResponseResult;
import com.cetc32.dh.entity.DataFile;
import com.cetc32.dh.entity.DataMenu;
import com.cetc32.dh.service.DataMenuService;
import com.cetc32.dh.dto.DataMenuDTO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据管理目录类
 * @author: xiao
 * @version: 1.0
 * @date: 2020/10/14
 * 备注:无
 */
@Slf4j
@RestController
@RequestMapping("/rest/datamng/menu")
public class DataMenuController {

    @Autowired
    private DataMenuService dataMenuService;

    /**
     * 统计目录节点的个数
     * @return ResponseResult
     * 备注:无
     */
    @PostMapping(value = "/count")
    public ResponseResult count() {
        Integer count = dataMenuService.count();
        return ResponseResult.success(count);
    }

    /**
     * 插入目录节点
     * @param dataMenu
     * @return ResponseResult
     * 备注:无
     */
    //@PostMapping(value = "/insert")
    public ResponseResult insert(DataMenu dataMenu) {
//        DataMenu dataMenu=new DataMenu(2L,"test7",2L,"test7","test7");
        Integer count = dataMenuService.insertDataMenu(dataMenu);
        return ResponseResult.success(count);
    }

    /**
     * 根据节点为id的所有目录节点
     * @param id
     * @return ResponseResult
     * 备注:无
     */
    //@PostMapping(value = "/select")
    public ResponseResult select(Long id) {

        DataMenu dataMenu = dataMenuService.queryById(id);
        if(dataMenu==null){
            return ResponseResult.error("没有id="+id+"的datamenu！");
        }
        return ResponseResult.success(dataMenu);
    }

    /**
     * 删除节点为id的目录节点(单个)
     * @param id
     * @return ResponseResult
     * 备注:无
     */
    //@PostMapping(value = "/deleteOne")
    public ResponseResult delete(Long id) {
        Integer count = dataMenuService.deleteById(id);
        return ResponseResult.success(count);
    }

    /**
     * 查询父节点为pid的所有目录节点
     * @param pid
     * @return ResponseResult
     * 备注:无
     */
    //@PostMapping(value = "/queryByPid")
    public ResponseResult queryByPid(Long pid) {
        DataMenu dataMenu = dataMenuService.queryByPid(pid);
        return ResponseResult.success(dataMenu);
    }

    /**
     * 查询目录节点menuId下的所有文件
     * @param menuId
     * @return ResponseResult
     * 备注:无
     */
    //@PostMapping(value = "/showFilesById")
    public ResponseResult showFilesById(Long menuId) {
        List<DataFile> dataFileList=dataMenuService.showFilesByMenuId(menuId);
        return ResponseResult.success(dataFileList);
    }

    /**
     * 查询目录树节点中以id为父节点的目录节点
     * @param id
     * @return ResponseResult
     * 备注:无
     */
    //@PostMapping(value = "/queryMenusByPIdSatisfyId")
    public ResponseResult queryMenusByPIdSatisfyId(Long id) {
        List<DataMenu> dataMenuList = dataMenuService.queryByPIdSatisfyId(id);
        return ResponseResult.success(dataMenuList);
    }

    /**
     * 添加目录节点
     * @param dataMenu
     * @return ResponseResult
     * 备注:无
     */
    //@PostMapping(value = "/addOneMenu")
    public ResponseResult addOneMenu(DataMenu dataMenu ) {
//        dataMenu = new DataMenu(null, "test7", null, "test15", "test15");
        String message=dataMenuService.addDataMenu(dataMenu);
        return ResponseResult.success(message);
    }

    /**
     * 根据目录节点id，更新目录节点
     * @param dataMenu
     * @return ResponseResult
     * 备注:无
     */
    //@PostMapping(value = "/updateDataMenu")
    public ResponseResult updateDataMenu(DataMenu dataMenu) {
//        dataMenu=new DataMenu(5L,"test5",1L,"test5","test5555");
        Integer count = dataMenuService.updatebyId(dataMenu);
        return ResponseResult.success(count);
    }

    /**
     * 根据目录节点id，删除目录树(包括子树)
     * @param id
     * @return ResponseResult
     * 备注:无
     */
    //@PostMapping(value = "/deleteMenuTree")
    public ResponseResult deleteDataMenuTree(Long id) {
        Integer count = dataMenuService.deleteMenuTree(id);
        System.out.println(count);
        DataMenuDTO dataMenuDTO = dataMenuService.getMenuTree(0l);
        return ResponseResult.success(dataMenuDTO);

    }

    /**
     * 根据目录节点id展示目录结构
     * @param id
     * @return ResponseResult
     * 备注:无
     */
    //@PostMapping(value = "/showMenuTree")
    public ResponseResult  showMenuTree(Long id) {
            if (StringUtils.isEmpty(id)) {
                id = 0L;
            }
            DataMenuDTO dataMenuDTO = dataMenuService.getMenuTree(id);
            return ResponseResult.success(dataMenuDTO);
    }

    /**
     * (目录名称)编目名称和编目id
     * @return 返回编目名称和编目id
     * */
    @ApiOperation(value="查询编目名称和对应的id")
    @RequestMapping(value = "/menuidname", method = RequestMethod.POST)
    public ResponseResult menuIdName() {
        List<DataMenu> dataMenuList = dataMenuService.selectAll();
        List<Map> hashMapList=new ArrayList<>();
        for(int i=0;i<dataMenuList.size();i++){
            Map<String,String> hashmap = new HashMap<>();
            hashmap.put("value",dataMenuList.get(i).getId().toString());
            hashmap.put("label",dataMenuList.get(i).getMenuName());
            hashMapList.add(hashmap);
        }
        return ResponseResult.success(hashMapList);
    }
}


